// 用户相关的小仓库
import { defineStore } from "pinia";
// 引入接口
import { reqLogin } from "@/api/admin/user/index";
// 引入数据类型
import type { LoginForm, LoginResponseData } from "@/api/admin/user/type";
import type { UserState } from "./type";

const useUserStore = defineStore("User", {
  state: (): UserState => {
    return {
      token: "", //用户唯一标识
    };
  },
  getters: {},
  actions: {
    // 登录
    async userLogin(data: LoginForm) {
      let res: LoginResponseData = await reqLogin(data);
      if (res.code == 200) {
        // 存储token
        this.token = res.data.token as string;
        return this.token;
      }
      return Promise.reject(new Error(res.msg));
    },
  },
  //开启本地存储
  persist: {
    //默认名称为当前store唯一标识 这里即home
    key: "user",
    //默认 localStorage 本地储存
    //这里建议临时储存sessionStorage 也可写成window.sessionStorage
    storage: sessionStorage,
    //默认当前store里的所有变量都持久化
    // paths: ["num"],
  },
});

export default useUserStore;
